#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

const int N = 1e5 + 10;
int e[N], ne[N], h[N], idx;
bool st[N];
int dist[N];
int n, m;

void add(int a, int b)
{
	e[idx] = b; ne[idx] = h[a]; h[a] = idx++;
}

void  bfs()
{
	int res = 0;
	queue<int> q;
	dist[1] = 0;
	st[1] = 1;
	while (q.size())
	{
		int t = q.front();
		q.pop();
		for (int i = h[1]; i != -1; i = ne[i])
		{
			int j = e[i];
			if (!st[j])
			{
				dist[j] = dist[i] + 1;
				q.push(j);
				st[j] = 1;
			}
		}
	}
}

int main()
{
	cin >> m >> n;
	memset(h, -1, sizeof h);
	memset(dist, 0x3f, sizeof dist);
	for (int i = 0; i < n - 1; i++)
	{
		int a, b;
		cin >> a >> b;
		add(a, b);
	}
	bfs();
	if (dist[n] != 0x3f3f3f3f) { cout << dist[n]; }
	else  cout << -1;
	return 0;
}